package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.CommonPhrasesMapper;
import com.zhijian.medical.entity.po.CommonPhrases;
import com.zhijian.medical.entity.vo.request.CommonPhrasesSearchVo;
import com.zhijian.medical.entity.vo.request.CommonPhrasesSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.CommonPhrasesVo;
import com.zhijian.medical.entity.vo.response.TotalCommonPhrasesVo;
import com.zhijian.medical.service.CommonPhrasesService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理常用语业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-07-24 13:09:08
 */
@Slf4j
@Service
public class CommonPhrasesServiceImpl implements CommonPhrasesService {

	@Autowired
    private CommonPhrasesMapper mapper;

    @Override
    public CommonPhrases selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonPhrasesVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(CommonPhrases commonPhrases) {
        Date now = new Date();
        commonPhrases.setCreateTime(now);
        commonPhrases.setUpdateTime(now);
        return mapper.insert(commonPhrases) > 0 ? commonPhrases.getId() : 0L;
    }

    @Override
    public Long insertSelective(CommonPhrases commonPhrases) {
        Date now = new Date();
        commonPhrases.setCreateTime(now);
        commonPhrases.setUpdateTime(now);
        return mapper.insertSelective(commonPhrases) > 0 ? commonPhrases.getId() : 0L;
    }

    @Override
    public boolean update(CommonPhrases commonPhrases) {
        Date now = new Date();
        commonPhrases.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(commonPhrases) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CommonPhrases record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(CommonPhrasesSaveOrUpdateVo saveOrUpdateVo) {
        CommonPhrases commonPhrases = new CommonPhrases();
        BeanUtils.copyProperties(saveOrUpdateVo, commonPhrases);
        return this.insert(commonPhrases) != 0;
    }

    @Override
    public boolean modify(CommonPhrasesSaveOrUpdateVo saveOrUpdateVo) {
        CommonPhrases old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        CommonPhrases commonPhrases = new CommonPhrases();
        BeanUtils.copyProperties(saveOrUpdateVo, commonPhrases);
        return this.update(commonPhrases);
    }

    @Override
    public CommonPhrasesVo selectOne(CommonPhrasesSearchVo commonPhrasesSearchVo) {
        return mapper.selectOne(commonPhrasesSearchVo);
     }

    @Override
    public Integer count(CommonPhrasesSearchVo commonPhrasesSearchVo) {
        return mapper.count(commonPhrasesSearchVo);
    }

    @Override
    public List<CommonPhrasesVo> selectList(CommonPhrasesSearchVo commonPhrasesSearchVo) {
        return mapper.selectList(commonPhrasesSearchVo);
    }

    @Override
    public List<TotalCommonPhrasesVo> selectAllPhrases() {
        // 筛出所有的根目录层级
        List<CommonPhrasesVo> commonPhrasesVos = mapper.selectList(CommonPhrasesSearchVo.builder().parentId(0L).build());
        List<TotalCommonPhrasesVo> totalCommonPhrasesVoList = new ArrayList<>();
        // 根据根目录 Id 找到对应的子集内容
        for (CommonPhrasesVo commonPhrasesVo : commonPhrasesVos) {
            TotalCommonPhrasesVo totalCommonPhrasesVo = new TotalCommonPhrasesVo();
            BeanUtils.copyProperties(commonPhrasesVo, totalCommonPhrasesVo);
            List<CommonPhrasesVo> phrasesVos = mapper.selectList(CommonPhrasesSearchVo.builder().parentId(commonPhrasesVo.getId()).build());
            totalCommonPhrasesVo.setCommonPhrasesList(phrasesVos);
            totalCommonPhrasesVoList.add(totalCommonPhrasesVo);
        }
        return totalCommonPhrasesVoList;
    }

    @Override
    public Page<CommonPhrasesVo> selectPage(CommonPhrasesSearchVo commonPhrasesSearchVo) {
        AssertUtil.check(Objects.isNull(commonPhrasesSearchVo.getPageNum()) || Objects.isNull(commonPhrasesSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(commonPhrasesSearchVo);
        //分页
        Page<CommonPhrasesVo> page = new Page<>(true, count, commonPhrasesSearchVo.getPageNum(), commonPhrasesSearchVo.getPageSize());
        commonPhrasesSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CommonPhrasesVo> responseVos = this.selectList(commonPhrasesSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CommonPhrases> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(CommonPhrasesSearchVo commonPhrasesSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(commonPhrasesSearchVo);
    }
    
}